<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Base;
use think\Db;
use think\Session;

class Activity extends Base
{
   public function lst()
   {
       $act=db('activity')->select();
       $this->assign('info',$act);
       return $this->fetch();
   }

   public function del()
   {
        $act_id=input('activity_id');
        $act_status=db('activity')->where('activity_id',$act_id)->field('activity_status')->find();
        if($act_status['activity_status']==2){
            return $this->error('活动进行中，无法删除活动！');
        }else{
            $del=db('activity')->where('activity_id',$act_id)->delete();
            $del=db('swiper')->where('activity_id',$act_id)->delete();
            if($del){
                return $this->success('删除活动成功！','lst');
            }else{
                return $this->error('删除活动失败！');
            }
        }
        return $this->fetch('lst');
   }

   
   public function add()
   {
    if(request()->isPost()){
        $file = request()->file('activity_pic');
        if($file){
            $info = $file->rule('uniqid')
                ->validate(['size'=>64000000,'ext'=>'jpg,png,gif'])
                ->move(ROOT_PATH . 'public' . DS . 'static'. DS .'images');
            if($info){
                $activity_pic=$info->getFilename();
            }
            else{
                echo $file->getError();
            }
        }else{
            $this->error('请选择活动图片！');
        }
        $data=[
            'activity_status'=>0,
            'activity_limit'=>input('activity_limit'),
            'activity_desc'=>input('activity_desc'),
            'activity_pic'=>$activity_pic,
        ];
        $res=db('activity')->insert($data);
        if($res){
            $this->success('添加活动成功！','lst');
        }else{
            $this->error('添加活动失败...');
        }
       
    }
     return $this->fetch();
   }


   public function edit()
   {
        $act_id=input('activity_id');
        $act_inf=db('activity')->where('activity_id',$act_id)->select();
        $this->assign('info',$act_inf);
        return $this->fetch();
   }


   public function doedit()
   {
    $activity_id=input('activity_id');
    if(request()->isPost()){
        $file = request()->file('activity_pic');
        if($file){
            $info = $file->rule('uniqid')
                ->validate(['size'=>64000000,'ext'=>'jpg,png,gif'])
                ->move(ROOT_PATH . 'public' . DS . 'static'. DS .'images');
            if($info){
                $activity_pic=$info->getFilename();
                $data=[
                    'activity_limit'=>input('activity_limit'),
                    'activity_desc'=>input('activity_desc'),
                    'activity_pic'=>$activity_pic,
                ];
                $res=db('activity')->where('activity_id',$activity_id)->update($data);
                if($res){
                    $this->success('修改活动信息成功！','lst');
                }else{
                    $this->error('修改活动信息失败...');
                }
            }else{
                echo $file->getError();
            }
        }else{
            $data=[
                'activity_status'=>0,
                'activity_limit'=>input('activity_limit'),
                'activity_desc'=>input('activity_desc'),
            ];
            $res=db('activity')->where('activity_id',$activity_id)->update($data);
            if($res){
                $this->success('修改活动信息成功！','lst');
            }else{
                $this->error('修改活动信息失败...');
            }
        }
    }
   }


   public function checkstatus()
   {
       $act_id=input('activity_id');
       $act_status=db('activity')->where('activity_id',$act_id)->field('activity_status')->find();
       if($act_status['activity_status']==0)
       {
            $act_status=['activity_status'=>1];
            $res=db('activity')->where('activity_id',$act_id)->update($act_status);
            if($res)
            {
                return $this->success('发布活动成功！','lst');
            }
            else{
                return $this->error('发布活动失败！');
            }
       }
       else if($act_status['activity_status']==1)
       {
            $act_status=['activity_status'=>2];
            $red=db('activity')->where('activity_id',$act_id)->update($act_status);
            if($red)
            {
                return $this->success('更改活动状态成功！','lst');
            }
            else
            {
                return $this->error('更改活动状态失败！');
            }
       }
   }


   public function enter()
    {
        $activity_id=input('activity_id');
        $info=Db::name("enter")->alias('e')->join("user u","e.user_id=u.user_id")
        ->join("activity a","e.activity_id=a.activity_id")->where('a.activity_id',$activity_id)->select();
        $this->assign('info',$info);
        return $this->fetch();
    }

}